package com.mipo.dao.imp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.mipo.util.DBUtil;
import com.mipo.vo.Employee;

public class EmployeeDAOImp implements EmployeeDAO {

	public Employee query(String name, String pwd) throws Exception {
		Employee emp = null;
		String sql = "select * from employee where empName=? and qq=?";
		Connection conn = DBUtil.getConnection();
		PreparedStatement pst = conn.prepareStatement(sql);
		pst.setString(1, name);
		pst.setString(2, pwd);
		System.out.println("Employee:    "+sql);
		ResultSet rs = pst.executeQuery();
		if (rs.next()) {
			emp = new Employee(rs.getInt(1), rs.getString(2), rs.getString(3),
					rs.getString(4), rs.getString(5), rs.getString(6), rs
							.getString(7), rs.getString(8), rs.getString(9), rs
							.getString(10), rs.getInt(11), rs.getInt(12));
		}
		DBUtil.close(conn);
		return emp;
	}

	public List<Employee> queryEmps(Employee emp) throws Exception {
		List<Employee> emps = new ArrayList<Employee>();
		String sql = "select * from employee where empid<>?;";
		Connection conn = DBUtil.getConnection();
		PreparedStatement pst = conn.prepareStatement(sql);
		pst.setInt(1, emp.getEmpId());
		System.out.println("queryEmps:    "+sql);
		ResultSet rs = pst.executeQuery();
		while (rs.next()) {
			Employee em = new Employee(rs.getInt(1), rs.getString(2), rs
					.getString(3), rs.getString(4), rs.getString(5), rs
					.getString(6), rs.getString(7), rs.getString(8), rs
					.getString(9), rs.getString(10), rs.getInt(11), rs
					.getInt(12));
			emps.add(em);
		}
		System.out.println("emps: size  "+emps.size());
		DBUtil.close(conn);
		return emps;
	}
	
	public Employee query(int empId) throws Exception {
		Employee emp = null;
		String sql = "select * from employee where empId=?";
		Connection conn = DBUtil.getConnection();
		PreparedStatement pst = conn.prepareStatement(sql);
		pst.setInt(1, empId);
		System.out.println("Employee:    "+sql);
		ResultSet rs = pst.executeQuery();
		if (rs.next()) {
			emp = new Employee(rs.getInt(1), rs.getString(2), rs.getString(3),
					rs.getString(4), rs.getString(5), rs.getString(6), rs
							.getString(7), rs.getString(8), rs.getString(9), rs
							.getString(10), rs.getInt(11), rs.getInt(12));
		}
		DBUtil.close(conn);
		return emp;
	}
}
